//
//  CityListViewController.m
//  Funny
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 King. All rights reserved.
//

#import "CityListViewController.h"
#import "CityListModel.h"
#import "NSString+Characters.h"

@interface CityListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) CityListModel *cityModel;
@property (nonatomic, strong) NSMutableDictionary *contacDic;
@property (nonatomic, strong) NSMutableArray *firstLArr;
@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UILabel *cityLable;
@property (nonatomic, strong) NSMutableArray *positionArr;
@property (nonatomic, strong) NSMutableDictionary *positionDic;
@end

@implementation CityListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"其他城市";

    self.positionArr = [NSMutableArray array];
    [self.view addSubview:self.iconImageView];
    [self.view addSubview:self.cityLable];
    [self handleData];
    [self.view addSubview:self.tableView];
}

- (UITableView *)tableView
{
    if (!_tableView) {
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, self.view.bounds.size.width, self.view.bounds.size.height-100) style:UITableViewStylePlain];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"tableView"];
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *key = [self.firstLArr objectAtIndex:section];
    NSMutableArray *cityArr = [self.contacDic objectForKey:key];
    return cityArr.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.firstLArr.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [self.firstLArr objectAtIndex:section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableView"];
    NSString *key = [self.firstLArr objectAtIndex:indexPath.section];
    NSMutableArray *cityArr = [self.contacDic objectForKey:key];
    self.cityModel = [cityArr objectAtIndex:indexPath.row];
    cell.textLabel.text = self.cityModel.cityNameAbbr;
    return cell;
}

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.firstLArr;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *key = [self.firstLArr objectAtIndex:indexPath.section];
    NSMutableArray *cityArr = [self.contacDic objectForKey:key];
    self.cityModel = [cityArr objectAtIndex:indexPath.row];
    [self.delegate passValue:self.cityModel.cityCode];
    [self.navigationController popViewControllerAnimated:YES];
}

- (UIImageView *)iconImageView
{
    if (!_iconImageView) {
        self.iconImageView = [[UIImageView alloc]init];
        [self.iconImageView setImage:[UIImage imageNamed:@"iconfont-dingwei-1.png"]];
        self.iconImageView.tintColor = [UIColor redColor];
        self.iconImageView.frame = CGRectMake(10, 10, 30, 30);
    }
    return _iconImageView;
}

- (UILabel *)cityLable
{
    if (!_cityLable) {
        self.cityLable = [[UILabel alloc]init];
        self.cityLable.text = [NSString stringWithFormat:@"当前城市:    %@",self.name];
        self.cityLable.frame = CGRectMake(50, 10, 200, 30);
    }
    return _cityLable;
}

#pragma mark -处理数据
- (void)handleData
{
    NSString *str = @"http://appapi.yaochufa.com/v2/Position/GetCityList?machineCode=00000000-58ca-002b-e3ba-8a9569d38c36&version=4.4.0&system=android&channel=nearme";
    [HandleData handleDataWithUrl:str completion:^(id data) {
        self.contacDic = [NSMutableDictionary dictionary];
        self.positionDic = [NSMutableDictionary dictionary];
        for (NSDictionary *dic in [[data objectForKey:@"data"] objectForKey:@"positionCity"]) {
            self.cityModel = [CityListModel modelWithDic:dic];
            NSString *firstLetter = [self.cityModel.cityNameAbbr firstCharacterOfName];
            NSMutableArray *cityArr = [self.contacDic objectForKey:firstLetter];
            if (cityArr == nil) {
                cityArr = [NSMutableArray array];
                [self.contacDic setObject:cityArr forKey:firstLetter];
            }
            [cityArr addObject:self.cityModel];
            [self.positionDic setObject:self.cityModel.cityCode forKey:self.cityModel.cityNameAbbr];
            [self.positionArr addObject:self.positionDic];
        }
        self.firstLArr = [[self.contacDic allKeys] mutableCopy];
        // 排序
        [self.firstLArr sortUsingSelector:@selector(compare:)];
        [self.tableView reloadData];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
